var model = require('../model');
var Position = model.Position;

//# 职位信息
exports.index = function (req, res, next) {
    res.render('backend/position/index', {
        title: '职位信息'
    });
};


//# 职位列表
exports.list = function (req, res, next) {
    var pageInfo = {};

    pageInfo.pageIndex = parseInt(req.query.pageIndex) || 1;
    pageInfo.pageSize = parseInt(req.query.pageSize) || 10;
    var skipIndex = (pageInfo.pageIndex - 1) * (pageInfo.pageSize);

    var titleReg = new RegExp(req.query.title);


    Position.count({ deleted: false, title: titleReg }, function (error, count) {
        if (error) {
            console.log(error);
        } else {
            pageInfo.count = count;
            pageInfo.totalPage = Math.ceil(count / pageInfo.pageSize);
        }
    });

    Position.find({ deleted: false, title: titleReg })
        .skip(skipIndex)
        .limit(pageInfo.pageSize)
        .exec(function (error, positions) {
            if (error) {
                console.log(error);
                return;
            }
            res.render('backend/position/list', {
                positions: positions,
                pageInfo: pageInfo
            });
        });
};

//# 添加职位界面
exports.new = function (req, res, next) {
    res.render('backend/position/new', {
        title: '添加职位'
    });
};

//# 创建职位
exports.create = function (req, res, next) {
    var title = req.body.title;
    var intro = req.body.intro;
    var position = new Position({
        title: title,
        intro: intro,
        deleted: false
    });
    position.save(function (error) {
        var result = {};
        if (error) {
            result.success = false;
            res.json(result);
        } else {
            result.success = true;
            res.append('Location', '/backend/positions');
            res.json(result);
        }
    });
};

//# 编辑页面
exports.edit = function (req, res, next) {
    var positionId = req.params.id;
    console.log(positionId);
    Position.findOne({ _id: positionId }, function (error, position) {
        if (error) {
            console.log(error);
        } else {
            res.render('backend/position/edit', {
                title: '编辑职位信息',
                position: position
            });
        }
    });
};

//# 更新职位
exports.update = function (req, res, next) {
    var title = req.body.title;
    var intro = req.body.intro;
    var positionId = req.params.id;

    var update = {
        title: title,
        intro: intro
    };

    Position.update({ _id: positionId }, update, function (error) {
        var result = {};
        if (error) {
            result.success = false;
            res.json(result);
        } else {
            result.success = true;
            res.json(result);
        }
    });
};

//# 更新职位
exports.delete = function (req, res, next) {
    var positionId = req.params.id;

    Position.update({ _id: positionId }, { deleted: true }, function (error) {
        var result = {};
        if (error) {
            result.success = false;
            res.json(result);
        } else {
            result.success = true;
            res.json(result);
        }
    });
};
